<?php

namespace app\teacher\controller\course;

use app\common\controller\Frontend;
use app\common\enums\StudentAppointmentStatusEnum;
use app\common\model\banxi\student\StudentCourseAppointment;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;

class StudentAppointment extends Frontend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
    }

    /*
     * 查询-学生预约课程列表
     * */
    public function listStudentAppointment()
    {
        $date = $this->request->get('date', '');
        if (!$date){
            $date = date('Y-m-d');
        }
        $trainerId = $this->auth->id;

        $StudentCourseAppointmentModel = new StudentCourseAppointment();
        $list = $StudentCourseAppointmentModel->with(['studentInfo'=> function ($query) {
            $query->field('id,name,avatar');
        }])->where(['date'=>$date,'trainerId'=>$trainerId,'appointmentStatus'=>StudentAppointmentStatusEnum::E1])->order('startTime','asc')->select();

        $this->ok('操作成功',$list);
    }

    /*
     * 处理学生预约课程
     * */
    public function handelStudentAppointment()
    {
        $param = $this->request->param(['id' => null, 'status' => 0]);

        try {
            $validate = Validate::rule([
                'id' => ValidateRule::isRequire(null, '参数错误'),
                'status' => ValidateRule::in([1, 2], '处理状态有误')
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $trainerId = $this->auth->id;
            $StudentCourseAppointmentModel = new StudentCourseAppointment();
            $StudentCourseAppointmentModel->handleAppointment($param['id'],  $trainerId, $param['status']);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功');
    }
}